package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.Result;
import com.heima.domain.dto.PageDTO;
import com.heima.domain.po.CheckupActivity;
import com.heima.domain.query.CheckupActivityQuery;
import com.heima.mapper.CheckupActivityMapper;
import com.heima.service.ICheckupActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 体检活动表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-28
 */
@Service
public class CheckupActivityServiceImpl extends ServiceImpl<CheckupActivityMapper, CheckupActivity> implements ICheckupActivityService {
    //查找状态为1的体检活动
    public Result findByPage(CheckupActivityQuery checkupActivityQuery){
        String name = checkupActivityQuery.getName();
        LocalDateTime beginTime = checkupActivityQuery.getBeginTime();
        LocalDateTime endTime = checkupActivityQuery.getEndTime();
        Integer pageNum= checkupActivityQuery.getPageNum();
        Integer pageSize= checkupActivityQuery.getPageSize();
        Page<CheckupActivity> p = this.lambdaQuery().like(StringUtils.isNotBlank(name), CheckupActivity::getName, name)
                .eq(CheckupActivity::getStatus, 1)
                .ge(beginTime != null, CheckupActivity::getStartTime, beginTime)
                .le(endTime != null, CheckupActivity::getStartTime, endTime)
                .orderByDesc(CheckupActivity::getStartTime)
                .page(new Page<>(pageNum, pageSize));
        List<CheckupActivity> records = p.getRecords();
        if (records.isEmpty()) { // 如果结果为空
            return Result
                    .success(new PageDTO<>(p.getTotal(), p.getSize(), p.getPages(), (long)pageNum, new ArrayList<>())); // 返回空数据的分页结果
        }
        return Result
                .success(new PageDTO<>(p.getTotal(), p.getSize(), p.getPages(), (long)pageSize, records)); // 返回查询到的数据分页结果
    }
}
